<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
use app\admin\model\product\Purity;
use app\admin\model\product\Category;
use app\admin\model\product\Material;
use app\admin\model\product\Technology;
use app\admin\model\product\Skn;
use JetBrains\PhpStorm\Internal\TentativeType;

class Auto extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        dump(123);
        Db::name('sms')->execute('truncate table fa_sms');
        return $this->view->fetch();
    }
    //产品分类
    public function auto_cate()
    {
        $product_category = DB::name('product')->group('product_category')->column('product_category');
        if($product_category){
            $narr = [];
            foreach($product_category as $v){
                $v = explode(',',$v);
                $narr += $v;
            }
            $narr = array_unique($narr);
        }
        dump($narr);die;
        $product_material = DB::name('product')->group('product_material')->column('product_material');
        
        $product_purity = DB::name('product')->group('product_purity')->column('product_purity');
        $product_technology = DB::name('product')->group('product_technology')->column('product_technology');
        $product_skn = DB::name('product')->group('product_skn')->column('product_skn');
        if (!empty($product_category)) {
            $old = Db::name('product_category')->column('name');
            $diff = array_diff($old, $product_category);
            foreach ($diff  as $v) {
                if (!in_array($v, $old)) {
                    $new[] = $v;
                } else {
                    $del[] = $v;
                }
            }
            Db::name('product_category')->whereIn('name', $del)->delete();
            (new Category())->saveAll($new);
        }
        if (!empty($product_material)) {
            $old = Db::name('product_material')->column('name');
            $diff = array_diff($old, $product_material);
            foreach ($diff  as $v) {
                if (!in_array($v, $old)) {
                    $new[] = $v;
                } else {
                    $del[] = $v;
                }
            }
            Db::name('product_material')->whereIn('name', $del)->delete();
            (new Material())->saveAll($new);
        }
        if (!empty($product_purity)) {
            $old = Db::name('product_purity')->column('name');
            $diff = array_diff($old, $product_purity);
            foreach ($diff  as $v) {
                if (!in_array($v, $old)) {
                    $new[] = $v;
                } else {
                    $del[] = $v;
                }
            }
            Db::name('product_purity')->whereIn('name', $del)->delete();
            (new Purity)->saveAll($new);
        }
        if (!empty($product_technology)) {
            $old = Db::name('product_technology')->column('name');
            $diff = array_diff($old, $product_technology);
            foreach ($diff  as $v) {
                if (!in_array($v, $old)) {
                    $new[] = $v;
                } else {
                    $del[] = $v;
                }
            }
            Db::name('product_technology')->whereIn('name', $del)->delete();
            (new Technology())->saveAll($new);
        }
        if (!empty($product_skn)) {
            $old = Db::name('product_skn')->column('name');
            $diff = array_diff($old, $product_skn);
            foreach ($diff  as $v) {
                if (!in_array($v, $old)) {
                    $new[] = $v;
                } else {
                    $del[] = $v;
                }
            }
            Db::name('product_skn')->whereIn('name', $del)->delete();
            (new Skn())->saveAll($new);
        }
        if (!empty($product_category)) {
            $old = Db::name('product_category')->column('name');
            $diff = array_diff($old, $product_category);
            foreach ($diff  as $v) {
                if (!in_array($v, $old)) {
                    $new[] = $v;
                } else {
                    $del[] = $v;
                }
            }
            Db::name('product_category')->whereIn('name', $del)->delete();
            (new Purity)->saveAll($new);
        }
    }
}
